﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Models;
using Service;

namespace DBDesign
{
    public partial class DbConnectionEdit : DevExpress.XtraEditors.XtraForm
    {
        private ClassStringService ClassStringService { get; set; }
        public DbConnectionEdit()
        {
            InitializeComponent();
        }

        private void DbConnectionEdit_Load(object sender, EventArgs e)
        {
            comboBoxEdit1.Properties.Items.Clear();
            comboBoxEdit1.Properties.Items.AddRange(ClassStringService.getSqlSugarDbType());
        }
        private int Id { get; set; }
        public DbConnectionEntity ConnectionEntity 
        {
            set
            {
                if (value!=null)
                {
                    textEdit1.Text = value.Text;
                    textEdit2.Text = value.ConStr;
                    comboBoxEdit1.SelectedText = value.DbType;
                    Id = value.Id;
                }
            }
            get
            {
                return new DbConnectionEntity() { 
                    Id = Id, 
                    DbType = comboBoxEdit1.SelectedText, 
                    Text = textEdit1.Text, 
                    ConStr = textEdit2.Text 
                };
            }
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(textEdit1.Text))
            {
                textEdit1.Focus();
                return;
            }
            if (string.IsNullOrWhiteSpace(comboBoxEdit1.Text))
            {
                comboBoxEdit1.Focus();
                return;
            }
            if (string.IsNullOrWhiteSpace(textEdit2.Text))
            {
                textEdit2.Focus();
                return;
            }
            this.DialogResult = DialogResult.OK;
        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }
    }
}